<%@page import="com.glowdayz.glowmee.dao.ProductDAO"%>
<%@page import="com.glowdayz.glowmee.cmd.ProductCMD"%>
<%@page import="com.glowdayz.common.PageHandler"%>
<%@page import="com.glowdayz.common.MultipartBox.FileInfo"%>
<%@page import="java.util.ArrayList"%>
<%@page import="com.glowdayz.common.DateTime"%>
<%@page import="com.glowdayz.common.ApiHandler"%>
<%@page import="com.glowdayz.common.DBHandler"%>
<%@ page language="java" contentType="text/xml; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page trimDirectiveWhitespaces="true"%>
<%
	DBHandler.getInstance().connect();
	
	ApiHandler web = new ApiHandler(request, out, "JSPglowmee");
	
	int nowPage = web.getInt("page");
	int count = web.getInt("count");
	String searchText = web.getValue("searchText");
	
	String whereValue = "";
	
	if (!searchText.equals("")) {
		String[] searchArr = searchText.split(" ");
		for (int i=0; i<searchArr.length; i++) {
			whereValue += " AND (p.productTitle like '%" + searchArr[i] + "%' OR b.brandTitle like '%" + searchArr[i] + "%')";
		}
		/* whereValue += " AND (replace(p.productTitle,' ','') like '%" + searchText + "%' OR p.productTitle like '%" + searchText + 
				"%' OR b.brandTitle like '%" + searchText + "%')"; */
	}
	
	ArrayList<ProductCMD> list = ProductDAO.getInstance().getSearchProductList(nowPage, count, whereValue);
	
	if (list == null) {
		DBHandler.getInstance().disconnect();
		web.result(null, "데이터 조회에 실패했습니다. 잠시후 다시 시도해 주세요.");
		return;
	}
	
	PageHandler p = PageHandler.getInstance();
	DBHandler.getInstance().disconnect();
%>
<?xml version="1.0" encoding="utf-8" ?>
<rss version="2.0">
	<channel>
		<!-- ##### 이 데이터를 소개하는 부분 (필수항목들) ##### -->
		<!-- 제목 -->
		<title>글로우미</title>
		<!-- XML 생성일시 -->
		<pubDate><%=DateTime.getInstance().getNowDateTimeString()%></pubDate>
		
		<!-- ##### 추가적인 정보들 (일반적으로 페이징 처리에 관련된 변수들) ##### -->
		<!-- 태그이름은 멤버변수이름, 값은 getter 호출 -->
		<nowPage><%=p.getNowPage()%></nowPage>
		<totalArticleCount><%=p.getTotalArticleCount()%></totalArticleCount>
		<pageArticleCount><%=p.getPageArticleCount()%></pageArticleCount>
		<pageCount><%=p.getPageCount()%></pageCount>
		<pageGroupCount><%=p.getPageGroupCount()%></pageGroupCount>
		<startPage><%=p.getStartPage()%></startPage>
		<endPage><%=p.getEndPage()%></endPage>
		<startRec><%=p.getStartRec()%></startRec>
		<lastGroupFirstPage><%=p.getLastGroupFirstPage()%></lastGroupFirstPage>
		
		<!-- ##### 게시물 정보 반복 처리 ##### -->
		<!-- 추가적인 정보를 덧붙이거나, 필요 없는 정보는 삭제 가능함 -->
		<%
			if (list != null) {
				for (int i=0; i<list.size(); i++) {
					ProductCMD item = list.get(i);
					FileInfo brandFile = item.getImage();
					FileInfo productFile = item.getProductImg();
		%>
		<item>
			<!-- 제품 항번 -->
			<idProduct><![CDATA[<%=item.getIdProduct()%>]]></idProduct>
			<!-- 제품명 -->
			<productTitle><![CDATA[<%=item.getProductTitle()%>]]></productTitle>
			<!-- 브랜드 항번 -->
			<idBrand><![CDATA[<%=item.getIdBrand()%>]]></idBrand>
			<!-- 용량 -->
			<volume><![CDATA[<%=item.getVolume()%>]]></volume>
			<!-- 가격 -->
			<price><![CDATA[<%=item.getPrice()%>]]></price>
			<!-- 브랜드 이미지 -->
			<%
			if (brandFile != null) {
			%>
			<brandImage><![CDATA[/glowmee/download.jsp?<%=brandFile.toString("utf-8")%>]]></brandImage>
			<brandThumbnail><![CDATA[/glowmee/download.jsp?<%=brandFile.toString("utf-8")%>&size=160]]></brandThumbnail>
			<%} else {%>
			<image>null</image>
			<thumbnail>null</thumbnail>
			<%}%>
			<!-- 제품 이미지 -->
			<%
			if (productFile != null) {
			%>
			<productImage><![CDATA[/glowmee/download.jsp?<%=productFile.toString("utf-8")%>]]></productImage>
			<productThumbnail><![CDATA[/glowmee/download.jsp?<%=productFile.toString("utf-8")%>&size=100]]></productThumbnail>
			<%} else {%>
			<productImage>null</productImage>
			<productThumbnail>null</productThumbnail>
			<%}%>
		</item>
		<%
				}
			}
		%>
	</channel>
</rss>